﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using HslCommunication.BasicFramework;
using HslCommunication.Core.Address;

namespace HslCommunication.Profinet.Melsec
{
	/// <summary>
	/// 所有三菱通讯类的通用辅助工具类，包含了一些通用的静态方法，可以使用本类来获取一些原始的报文信息。详细的操作参见例子<br />
	/// All general auxiliary tool classes of Mitsubishi communication class include some general static methods. 
	/// You can use this class to get some primitive message information. See the example for detailed operation
	/// </summary>
	// Token: 0x0200006C RID: 108
	public class MelsecHelper
	{
		/// <summary>
		/// 解析A1E协议数据地址<br />
		/// Parse A1E protocol data address
		/// </summary>
		/// <param name="address">数据地址</param>
		/// <returns>结果对象</returns>
		// Token: 0x060008C5 RID: 2245 RVA: 0x00030EF4 File Offset: 0x0002F0F4
		public static OperateResult<MelsecA1EDataType, int> McA1EAnalysisAddress(string address)
		{
			OperateResult<MelsecA1EDataType, int> operateResult = new OperateResult<MelsecA1EDataType, int>();
			try
			{
				char c = address[0];
				char c2 = c;
				if (c2 <= 'Y')
				{
					switch (c2)
					{
					case 'B':
						goto IL_406;
					case 'C':
						goto IL_1D5;
					case 'D':
						goto IL_435;
					case 'E':
						goto IL_4B9;
					case 'F':
						goto IL_3D7;
					default:
						switch (c2)
						{
						case 'M':
							goto IL_379;
						case 'N':
						case 'O':
						case 'P':
						case 'Q':
						case 'U':
						case 'V':
							goto IL_4B9;
						case 'R':
							goto IL_461;
						case 'S':
							goto IL_3A8;
						case 'T':
							break;
						case 'W':
							goto IL_48D;
						case 'X':
							goto IL_2D3;
						case 'Y':
							goto IL_326;
						default:
							goto IL_4B9;
						}
						break;
					}
				}
				else
				{
					switch (c2)
					{
					case 'b':
						goto IL_406;
					case 'c':
						goto IL_1D5;
					case 'd':
						goto IL_435;
					case 'e':
						goto IL_4B9;
					case 'f':
						goto IL_3D7;
					default:
						switch (c2)
						{
						case 'm':
							goto IL_379;
						case 'n':
						case 'o':
						case 'p':
						case 'q':
						case 'u':
						case 'v':
							goto IL_4B9;
						case 'r':
							goto IL_461;
						case 's':
							goto IL_3A8;
						case 't':
							break;
						case 'w':
							goto IL_48D;
						case 'x':
							goto IL_2D3;
						case 'y':
							goto IL_326;
						default:
							goto IL_4B9;
						}
						break;
					}
				}
				bool flag = address[1] == 'S' || address[1] == 's';
				if (flag)
				{
					operateResult.Content1 = MelsecA1EDataType.TS;
					operateResult.Content2 = Convert.ToInt32(address.Substring(2), MelsecA1EDataType.TS.FromBase);
				}
				else
				{
					bool flag2 = address[1] == 'C' || address[1] == 'c';
					if (flag2)
					{
						operateResult.Content1 = MelsecA1EDataType.TC;
						operateResult.Content2 = Convert.ToInt32(address.Substring(2), MelsecA1EDataType.TC.FromBase);
					}
					else
					{
						bool flag3 = address[1] == 'N' || address[1] == 'n';
						if (!flag3)
						{
							throw new Exception(StringResources.Language.NotSupportedDataType);
						}
						operateResult.Content1 = MelsecA1EDataType.TN;
						operateResult.Content2 = Convert.ToInt32(address.Substring(2), MelsecA1EDataType.TN.FromBase);
					}
				}
				goto IL_4C9;
				IL_1D5:
				bool flag4 = address[1] == 'S' || address[1] == 's';
				if (flag4)
				{
					operateResult.Content1 = MelsecA1EDataType.CS;
					operateResult.Content2 = Convert.ToInt32(address.Substring(2), MelsecA1EDataType.CS.FromBase);
				}
				else
				{
					bool flag5 = address[1] == 'C' || address[1] == 'c';
					if (flag5)
					{
						operateResult.Content1 = MelsecA1EDataType.CC;
						operateResult.Content2 = Convert.ToInt32(address.Substring(2), MelsecA1EDataType.CC.FromBase);
					}
					else
					{
						bool flag6 = address[1] == 'N' || address[1] == 'n';
						if (!flag6)
						{
							throw new Exception(StringResources.Language.NotSupportedDataType);
						}
						operateResult.Content1 = MelsecA1EDataType.CN;
						operateResult.Content2 = Convert.ToInt32(address.Substring(2), MelsecA1EDataType.CN.FromBase);
					}
				}
				goto IL_4C9;
				IL_2D3:
				operateResult.Content1 = MelsecA1EDataType.X;
				address = address.Substring(1);
				bool flag7 = address.StartsWith("0");
				if (flag7)
				{
					operateResult.Content2 = Convert.ToInt32(address, 8);
				}
				else
				{
					operateResult.Content2 = Convert.ToInt32(address, MelsecA1EDataType.X.FromBase);
				}
				goto IL_4C9;
				IL_326:
				operateResult.Content1 = MelsecA1EDataType.Y;
				address = address.Substring(1);
				bool flag8 = address.StartsWith("0");
				if (flag8)
				{
					operateResult.Content2 = Convert.ToInt32(address, 8);
				}
				else
				{
					operateResult.Content2 = Convert.ToInt32(address, MelsecA1EDataType.Y.FromBase);
				}
				goto IL_4C9;
				IL_379:
				operateResult.Content1 = MelsecA1EDataType.M;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.M.FromBase);
				goto IL_4C9;
				IL_3A8:
				operateResult.Content1 = MelsecA1EDataType.S;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.S.FromBase);
				goto IL_4C9;
				IL_3D7:
				operateResult.Content1 = MelsecA1EDataType.F;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.F.FromBase);
				goto IL_4C9;
				IL_406:
				operateResult.Content1 = MelsecA1EDataType.B;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.B.FromBase);
				goto IL_4C9;
				IL_435:
				operateResult.Content1 = MelsecA1EDataType.D;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.D.FromBase);
				goto IL_4C9;
				IL_461:
				operateResult.Content1 = MelsecA1EDataType.R;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.R.FromBase);
				goto IL_4C9;
				IL_48D:
				operateResult.Content1 = MelsecA1EDataType.W;
				operateResult.Content2 = Convert.ToInt32(address.Substring(1), MelsecA1EDataType.W.FromBase);
				goto IL_4C9;
				IL_4B9:
				throw new Exception(StringResources.Language.NotSupportedDataType);
				IL_4C9:;
			}
			catch (Exception ex)
			{
				operateResult.Message = ex.Message;
				return operateResult;
			}
			operateResult.IsSuccess = true;
			return operateResult;
		}

		/// <summary>
		/// 从三菱地址，是否位读取进行创建读取的MC的核心报文<br />
		/// From the Mitsubishi address, whether to read the core message of the MC for creating and reading
		/// </summary>
		/// <param name="isBit">是否进行了位读取操作</param>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008C6 RID: 2246 RVA: 0x00031410 File Offset: 0x0002F610
		public static byte[] BuildReadMcCoreCommand(McAddressData addressData, bool isBit)
		{
			return new byte[]
			{
				1,
				4,
				isBit ? 1 : 0,
				0,
				BitConverter.GetBytes(addressData.AddressStart)[0],
				BitConverter.GetBytes(addressData.AddressStart)[1],
				BitConverter.GetBytes(addressData.AddressStart)[2],
				addressData.McDataType.DataCode,
				(byte)(addressData.Length % 256),
				(byte)(addressData.Length / 256)
			};
		}

		/// <summary>
		/// 从三菱地址，是否位读取进行创建读取Ascii格式的MC的核心报文
		/// </summary>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <param name="isBit">是否进行了位读取操作</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008C7 RID: 2247 RVA: 0x000314A0 File Offset: 0x0002F6A0
		public static byte[] BuildAsciiReadMcCoreCommand(McAddressData addressData, bool isBit)
		{
			return new byte[]
			{
				48,
				52,
				48,
				49,
				48,
				48,
				48,
				isBit ? 49 : 48,
				Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[0],
				Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[1],
				MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[0],
				MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[1],
				MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[2],
				MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[3],
				MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[4],
				MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[5],
				SoftBasic.BuildAsciiBytesFrom(addressData.Length)[0],
				SoftBasic.BuildAsciiBytesFrom(addressData.Length)[1],
				SoftBasic.BuildAsciiBytesFrom(addressData.Length)[2],
				SoftBasic.BuildAsciiBytesFrom(addressData.Length)[3]
			};
		}

		/// <summary>
		/// 以字为单位，创建数据写入的核心报文
		/// </summary>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <param name="value">实际的原始数据信息</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008C8 RID: 2248 RVA: 0x000315F0 File Offset: 0x0002F7F0
		public static byte[] BuildWriteWordCoreCommand(McAddressData addressData, byte[] value)
		{
			bool flag = value == null;
			if (flag)
			{
				value = new byte[0];
			}
			byte[] array = new byte[10 + value.Length];
			array[0] = 1;
			array[1] = 20;
			array[2] = 0;
			array[3] = 0;
			array[4] = BitConverter.GetBytes(addressData.AddressStart)[0];
			array[5] = BitConverter.GetBytes(addressData.AddressStart)[1];
			array[6] = BitConverter.GetBytes(addressData.AddressStart)[2];
			array[7] = addressData.McDataType.DataCode;
			array[8] = (byte)(value.Length / 2 % 256);
			array[9] = (byte)(value.Length / 2 / 256);
			value.CopyTo(array, 10);
			return array;
		}

		/// <summary>
		/// 以字为单位，创建ASCII数据写入的核心报文
		/// </summary>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <param name="value">实际的原始数据信息</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008C9 RID: 2249 RVA: 0x00031698 File Offset: 0x0002F898
		public static byte[] BuildAsciiWriteWordCoreCommand(McAddressData addressData, byte[] value)
		{
			value = MelsecHelper.TransByteArrayToAsciiByteArray(value);
			byte[] array = new byte[20 + value.Length];
			array[0] = 49;
			array[1] = 52;
			array[2] = 48;
			array[3] = 49;
			array[4] = 48;
			array[5] = 48;
			array[6] = 48;
			array[7] = 48;
			array[8] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[0];
			array[9] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[1];
			array[10] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[0];
			array[11] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[1];
			array[12] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[2];
			array[13] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[3];
			array[14] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[4];
			array[15] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[5];
			array[16] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[0];
			array[17] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[1];
			array[18] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[2];
			array[19] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[3];
			value.CopyTo(array, 20);
			return array;
		}

		/// <summary>
		/// 以位为单位，创建数据写入的核心报文
		/// </summary>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <param name="value">原始的bool数组数据</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008CA RID: 2250 RVA: 0x000317F4 File Offset: 0x0002F9F4
		public static byte[] BuildWriteBitCoreCommand(McAddressData addressData, bool[] value)
		{
			bool flag = value == null;
			if (flag)
			{
				value = new bool[0];
			}
			byte[] array = MelsecHelper.TransBoolArrayToByteData(value);
			byte[] array2 = new byte[10 + array.Length];
			array2[0] = 1;
			array2[1] = 20;
			array2[2] = 1;
			array2[3] = 0;
			array2[4] = BitConverter.GetBytes(addressData.AddressStart)[0];
			array2[5] = BitConverter.GetBytes(addressData.AddressStart)[1];
			array2[6] = BitConverter.GetBytes(addressData.AddressStart)[2];
			array2[7] = addressData.McDataType.DataCode;
			array2[8] = (byte)(value.Length % 256);
			array2[9] = (byte)(value.Length / 256);
			array.CopyTo(array2, 10);
			return array2;
		}

		/// <summary>
		/// 以位为单位，创建ASCII数据写入的核心报文
		/// </summary>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <param name="value">原始的bool数组数据</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008CB RID: 2251 RVA: 0x000318A0 File Offset: 0x0002FAA0
		public static byte[] BuildAsciiWriteBitCoreCommand(McAddressData addressData, bool[] value)
		{
			bool flag = value == null;
			if (flag)
			{
				value = new bool[0];
			}
			byte[] array = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(value, (bool m) => m ? 49 : 48));
			byte[] array2 = new byte[20 + array.Length];
			array2[0] = 49;
			array2[1] = 52;
			array2[2] = 48;
			array2[3] = 49;
			array2[4] = 48;
			array2[5] = 48;
			array2[6] = 48;
			array2[7] = 49;
			array2[8] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[0];
			array2[9] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[1];
			array2[10] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[0];
			array2[11] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[1];
			array2[12] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[2];
			array2[13] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[3];
			array2[14] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[4];
			array2[15] = MelsecHelper.BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[5];
			array2[16] = SoftBasic.BuildAsciiBytesFrom((ushort)value.Length)[0];
			array2[17] = SoftBasic.BuildAsciiBytesFrom((ushort)value.Length)[1];
			array2[18] = SoftBasic.BuildAsciiBytesFrom((ushort)value.Length)[2];
			array2[19] = SoftBasic.BuildAsciiBytesFrom((ushort)value.Length)[3];
			array.CopyTo(array2, 20);
			return array2;
		}

		/// <summary>
		/// 从三菱扩展地址，是否位读取进行创建读取的MC的核心报文
		/// </summary>
		/// <param name="isBit">是否进行了位读取操作</param>
		/// <param name="extend">扩展指定</param>
		/// <param name="addressData">三菱Mc协议的数据地址</param>
		/// <returns>带有成功标识的报文对象</returns>
		// Token: 0x060008CC RID: 2252 RVA: 0x00031A28 File Offset: 0x0002FC28
		public static byte[] BuildReadMcCoreExtendCommand(McAddressData addressData, ushort extend, bool isBit)
		{
			return new byte[]
			{
				1,
				4,
				isBit ? 129 : 128,
				0,
				0,
				0,
				BitConverter.GetBytes(addressData.AddressStart)[0],
				BitConverter.GetBytes(addressData.AddressStart)[1],
				BitConverter.GetBytes(addressData.AddressStart)[2],
				addressData.McDataType.DataCode,
				0,
				0,
				BitConverter.GetBytes(extend)[0],
				BitConverter.GetBytes(extend)[1],
				249,
				(byte)(addressData.Length % 256),
				(byte)(addressData.Length / 256)
			};
		}

		/// <summary>
		/// 按字为单位随机读取的指令创建
		/// </summary>
		/// <param name="address">地址数组</param>
		/// <returns>指令</returns>
		// Token: 0x060008CD RID: 2253 RVA: 0x00031AF8 File Offset: 0x0002FCF8
		public static byte[] BuildReadRandomWordCommand(McAddressData[] address)
		{
			byte[] array = new byte[6 + address.Length * 4];
			array[0] = 3;
			array[1] = 4;
			array[2] = 0;
			array[3] = 0;
			array[4] = (byte)address.Length;
			array[5] = 0;
			for (int i = 0; i < address.Length; i++)
			{
				array[i * 4 + 6] = BitConverter.GetBytes(address[i].AddressStart)[0];
				array[i * 4 + 7] = BitConverter.GetBytes(address[i].AddressStart)[1];
				array[i * 4 + 8] = BitConverter.GetBytes(address[i].AddressStart)[2];
				array[i * 4 + 9] = address[i].McDataType.DataCode;
			}
			return array;
		}

		/// <summary>
		/// 随机读取的指令创建
		/// </summary>
		/// <param name="address">地址数组</param>
		/// <returns>指令</returns>
		// Token: 0x060008CE RID: 2254 RVA: 0x00031BA0 File Offset: 0x0002FDA0
		public static byte[] BuildReadRandomCommand(McAddressData[] address)
		{
			byte[] array = new byte[6 + address.Length * 6];
			array[0] = 6;
			array[1] = 4;
			array[2] = 0;
			array[3] = 0;
			array[4] = (byte)address.Length;
			array[5] = 0;
			for (int i = 0; i < address.Length; i++)
			{
				array[i * 6 + 6] = BitConverter.GetBytes(address[i].AddressStart)[0];
				array[i * 6 + 7] = BitConverter.GetBytes(address[i].AddressStart)[1];
				array[i * 6 + 8] = BitConverter.GetBytes(address[i].AddressStart)[2];
				array[i * 6 + 9] = address[i].McDataType.DataCode;
				array[i * 6 + 10] = (byte)(address[i].Length % 256);
				array[i * 6 + 11] = (byte)(address[i].Length / 256);
			}
			return array;
		}

		/// <summary>
		/// 按字为单位随机读取的指令创建
		/// </summary>
		/// <param name="address">地址数组</param>
		/// <returns>指令</returns>
		// Token: 0x060008CF RID: 2255 RVA: 0x00031C7C File Offset: 0x0002FE7C
		public static byte[] BuildAsciiReadRandomWordCommand(McAddressData[] address)
		{
			byte[] array = new byte[12 + address.Length * 8];
			array[0] = 48;
			array[1] = 52;
			array[2] = 48;
			array[3] = 51;
			array[4] = 48;
			array[5] = 48;
			array[6] = 48;
			array[7] = 48;
			array[8] = SoftBasic.BuildAsciiBytesFrom((byte)address.Length)[0];
			array[9] = SoftBasic.BuildAsciiBytesFrom((byte)address.Length)[1];
			array[10] = 48;
			array[11] = 48;
			for (int i = 0; i < address.Length; i++)
			{
				array[i * 8 + 12] = Encoding.ASCII.GetBytes(address[i].McDataType.AsciiCode)[0];
				array[i * 8 + 13] = Encoding.ASCII.GetBytes(address[i].McDataType.AsciiCode)[1];
				array[i * 8 + 14] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[0];
				array[i * 8 + 15] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[1];
				array[i * 8 + 16] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[2];
				array[i * 8 + 17] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[3];
				array[i * 8 + 18] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[4];
				array[i * 8 + 19] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[5];
			}
			return array;
		}

		/// <summary>
		/// 随机读取的指令创建
		/// </summary>
		/// <param name="address">地址数组</param>
		/// <returns>指令</returns>
		// Token: 0x060008D0 RID: 2256 RVA: 0x00031E04 File Offset: 0x00030004
		public static byte[] BuildAsciiReadRandomCommand(McAddressData[] address)
		{
			byte[] array = new byte[12 + address.Length * 12];
			array[0] = 48;
			array[1] = 52;
			array[2] = 48;
			array[3] = 54;
			array[4] = 48;
			array[5] = 48;
			array[6] = 48;
			array[7] = 48;
			array[8] = SoftBasic.BuildAsciiBytesFrom((byte)address.Length)[0];
			array[9] = SoftBasic.BuildAsciiBytesFrom((byte)address.Length)[1];
			array[10] = 48;
			array[11] = 48;
			for (int i = 0; i < address.Length; i++)
			{
				array[i * 12 + 12] = Encoding.ASCII.GetBytes(address[i].McDataType.AsciiCode)[0];
				array[i * 12 + 13] = Encoding.ASCII.GetBytes(address[i].McDataType.AsciiCode)[1];
				array[i * 12 + 14] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[0];
				array[i * 12 + 15] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[1];
				array[i * 12 + 16] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[2];
				array[i * 12 + 17] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[3];
				array[i * 12 + 18] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[4];
				array[i * 12 + 19] = MelsecHelper.BuildBytesFromAddress(address[i].AddressStart, address[i].McDataType)[5];
				array[i * 12 + 20] = SoftBasic.BuildAsciiBytesFrom(address[i].Length)[0];
				array[i * 12 + 21] = SoftBasic.BuildAsciiBytesFrom(address[i].Length)[1];
				array[i * 12 + 22] = SoftBasic.BuildAsciiBytesFrom(address[i].Length)[2];
				array[i * 12 + 23] = SoftBasic.BuildAsciiBytesFrom(address[i].Length)[3];
			}
			return array;
		}

		/// <summary>
		/// 创建批量读取标签的报文数据信息
		/// </summary>
		/// <param name="tags">标签名</param>
		/// <param name="lengths">长度信息</param>
		/// <returns>报文名称</returns>
		// Token: 0x060008D1 RID: 2257 RVA: 0x00031FF8 File Offset: 0x000301F8
		public static byte[] BuildReadTag(string[] tags, ushort[] lengths)
		{
			bool flag = tags.Length != lengths.Length;
			if (flag)
			{
				throw new Exception(StringResources.Language.TwoParametersLengthIsNotSame);
			}
			MemoryStream memoryStream = new MemoryStream();
			memoryStream.WriteByte(26);
			memoryStream.WriteByte(4);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(BitConverter.GetBytes(tags.Length)[0]);
			memoryStream.WriteByte(BitConverter.GetBytes(tags.Length)[1]);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			for (int i = 0; i < tags.Length; i++)
			{
				byte[] bytes = Encoding.Unicode.GetBytes(tags[i]);
				memoryStream.WriteByte(BitConverter.GetBytes(bytes.Length / 2)[0]);
				memoryStream.WriteByte(BitConverter.GetBytes(bytes.Length / 2)[1]);
				memoryStream.Write(bytes, 0, bytes.Length);
				memoryStream.WriteByte(1);
				memoryStream.WriteByte(0);
				memoryStream.WriteByte(BitConverter.GetBytes((int)(lengths[i] * 2))[0]);
				memoryStream.WriteByte(BitConverter.GetBytes((int)(lengths[i] * 2))[1]);
			}
			byte[] result = memoryStream.ToArray();
			memoryStream.Dispose();
			return result;
		}

		/// <summary>
		/// 解析出标签读取的数据内容
		/// </summary>
		/// <param name="content">返回的数据信息</param>
		/// <returns>解析结果</returns>
		// Token: 0x060008D2 RID: 2258 RVA: 0x0003212C File Offset: 0x0003032C
		public static OperateResult<byte[]> ExtraTagData(byte[] content)
		{
			OperateResult<byte[]> result;
			try
			{
				int num = (int)BitConverter.ToUInt16(content, 0);
				int num2 = 2;
				List<byte> list = new List<byte>(20);
				for (int i = 0; i < num; i++)
				{
					int num3 = (int)BitConverter.ToUInt16(content, num2 + 2);
					list.AddRange(SoftBasic.ArraySelectMiddle<byte>(content, num2 + 4, num3));
					num2 += 4 + num3;
				}
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message + " Source:" + SoftBasic.ByteToHexString(content, ' '));
			}
			return result;
		}

		/// <summary>
		/// 读取本站缓冲寄存器的数据信息，需要指定寄存器的地址，和读取的长度
		/// </summary>
		/// <param name="address">寄存器的地址</param>
		/// <param name="length">数据长度</param>
		/// <returns>结果内容</returns>
		// Token: 0x060008D3 RID: 2259 RVA: 0x000321CC File Offset: 0x000303CC
		public static OperateResult<byte[]> BuildReadMemoryCommand(string address, ushort length)
		{
			OperateResult<byte[]> result;
			try
			{
				uint value = uint.Parse(address);
				result = OperateResult.CreateSuccessResult<byte[]>(new byte[]
				{
					19,
					6,
					0,
					0,
					BitConverter.GetBytes(value)[0],
					BitConverter.GetBytes(value)[1],
					BitConverter.GetBytes(value)[2],
					BitConverter.GetBytes(value)[3],
					(byte)(length % 256),
					(byte)(length / 256)
				});
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecHelper.BuildReadMemoryCommand(System.String,System.UInt16)" />
		// Token: 0x060008D4 RID: 2260 RVA: 0x00032268 File Offset: 0x00030468
		public static OperateResult<byte[]> BuildAsciiReadMemoryCommand(string address, ushort length)
		{
			OperateResult<byte[]> result;
			try
			{
				uint value = uint.Parse(address);
				byte[] array = new byte[20];
				array[0] = 48;
				array[1] = 54;
				array[2] = 49;
				array[3] = 51;
				array[4] = 48;
				array[5] = 48;
				array[6] = 48;
				array[7] = 48;
				SoftBasic.BuildAsciiBytesFrom(value).CopyTo(array, 8);
				SoftBasic.BuildAsciiBytesFrom(length).CopyTo(array, 16);
				result = OperateResult.CreateSuccessResult<byte[]>(array);
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 构建读取智能模块的命令，需要指定模块编号，起始地址，读取的长度，注意，该长度以字节为单位。
		/// </summary>
		/// <param name="module">模块编号</param>
		/// <param name="address">智能模块的起始地址</param>
		/// <param name="length">读取的字长度</param>
		/// <returns>报文的结果内容</returns>
		// Token: 0x060008D5 RID: 2261 RVA: 0x000322F8 File Offset: 0x000304F8
		public static OperateResult<byte[]> BuildReadSmartModule(ushort module, string address, ushort length)
		{
			OperateResult<byte[]> result;
			try
			{
				uint value = uint.Parse(address);
				result = OperateResult.CreateSuccessResult<byte[]>(new byte[]
				{
					1,
					6,
					0,
					0,
					BitConverter.GetBytes(value)[0],
					BitConverter.GetBytes(value)[1],
					BitConverter.GetBytes(value)[2],
					BitConverter.GetBytes(value)[3],
					(byte)(length % 256),
					(byte)(length / 256),
					BitConverter.GetBytes(module)[0],
					BitConverter.GetBytes(module)[1]
				});
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecHelper.BuildReadSmartModule(System.UInt16,System.String,System.UInt16)" />
		// Token: 0x060008D6 RID: 2262 RVA: 0x000323AC File Offset: 0x000305AC
		public static OperateResult<byte[]> BuildAsciiReadSmartModule(ushort module, string address, ushort length)
		{
			OperateResult<byte[]> result;
			try
			{
				uint value = uint.Parse(address);
				byte[] array = new byte[24];
				array[0] = 48;
				array[1] = 54;
				array[2] = 48;
				array[3] = 49;
				array[4] = 48;
				array[5] = 48;
				array[6] = 48;
				array[7] = 48;
				SoftBasic.BuildAsciiBytesFrom(value).CopyTo(array, 8);
				SoftBasic.BuildAsciiBytesFrom(length).CopyTo(array, 16);
				SoftBasic.BuildAsciiBytesFrom(module).CopyTo(array, 20);
				result = OperateResult.CreateSuccessResult<byte[]>(array);
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 根据三菱的错误码去查找对象描述信息
		/// </summary>
		/// <param name="code">错误码</param>
		/// <returns>描述信息</returns>
		// Token: 0x060008D7 RID: 2263 RVA: 0x00032448 File Offset: 0x00030648
		public static string GetErrorDescription(int code)
		{
			string result;
			if (code != 2)
			{
				switch (code)
				{
				case 81:
					return StringResources.Language.MelsecError51;
				case 82:
					return StringResources.Language.MelsecError52;
				case 83:
				case 87:
					break;
				case 84:
					return StringResources.Language.MelsecError54;
				case 85:
					return StringResources.Language.MelsecError55;
				case 86:
					return StringResources.Language.MelsecError56;
				case 88:
					return StringResources.Language.MelsecError58;
				case 89:
					return StringResources.Language.MelsecError59;
				default:
					switch (code)
					{
					case 49229:
						return StringResources.Language.MelsecErrorC04D;
					case 49232:
						return StringResources.Language.MelsecErrorC050;
					case 49233:
					case 49234:
					case 49235:
					case 49236:
						return StringResources.Language.MelsecErrorC051_54;
					case 49237:
						return StringResources.Language.MelsecErrorC055;
					case 49238:
						return StringResources.Language.MelsecErrorC056;
					case 49239:
						return StringResources.Language.MelsecErrorC057;
					case 49240:
						return StringResources.Language.MelsecErrorC058;
					case 49241:
						return StringResources.Language.MelsecErrorC059;
					case 49242:
					case 49243:
						return StringResources.Language.MelsecErrorC05A_B;
					case 49244:
						return StringResources.Language.MelsecErrorC05C;
					case 49245:
						return StringResources.Language.MelsecErrorC05D;
					case 49246:
						return StringResources.Language.MelsecErrorC05E;
					case 49247:
						return StringResources.Language.MelsecErrorC05F;
					case 49248:
						return StringResources.Language.MelsecErrorC060;
					case 49249:
						return StringResources.Language.MelsecErrorC061;
					case 49250:
						return StringResources.Language.MelsecErrorC062;
					case 49264:
						return StringResources.Language.MelsecErrorC070;
					case 49266:
						return StringResources.Language.MelsecErrorC072;
					case 49268:
						return StringResources.Language.MelsecErrorC074;
					}
					break;
				}
				result = StringResources.Language.MelsecPleaseReferToManualDocument;
			}
			else
			{
				result = StringResources.Language.MelsecError02;
			}
			return result;
		}

		/// <summary>
		/// 从三菱的地址中构建MC协议的6字节的ASCII格式的地址
		/// </summary>
		/// <param name="address">三菱地址</param>
		/// <param name="type">三菱的数据类型</param>
		/// <returns>6字节的ASCII格式的地址</returns>
		// Token: 0x060008D8 RID: 2264 RVA: 0x000326E8 File Offset: 0x000308E8
		internal static byte[] BuildBytesFromAddress(int address, MelsecMcDataType type)
		{
			return Encoding.ASCII.GetBytes(address.ToString((type.FromBase == 10) ? "D6" : "X6"));
		}

		/// <summary>
		/// 将0，1，0，1的字节数组压缩成三菱格式的字节数组来表示开关量的
		/// </summary>
		/// <param name="value">原始的数据字节</param>
		/// <returns>压缩过后的数据字节</returns>
		// Token: 0x060008D9 RID: 2265 RVA: 0x00032721 File Offset: 0x00030921
		internal static byte[] TransBoolArrayToByteData(byte[] value)
		{
			return MelsecHelper.TransBoolArrayToByteData(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(value, (byte m) => m > 0)));
		}

		/// <summary>
		/// 将bool的组压缩成三菱格式的字节数组来表示开关量的
		/// </summary>
		/// <param name="value">原始的数据字节</param>
		/// <returns>压缩过后的数据字节</returns>
		// Token: 0x060008DA RID: 2266 RVA: 0x00032754 File Offset: 0x00030954
		internal static byte[] TransBoolArrayToByteData(bool[] value)
		{
			int num = (value.Length + 1) / 2;
			byte[] array = new byte[num];
			for (int i = 0; i < num; i++)
			{
				bool flag = value[i * 2];
				if (flag)
				{
					byte[] array2 = array;
					int num2 = i;
					array2[num2] += 16;
				}
				bool flag2 = i * 2 + 1 < value.Length;
				if (flag2)
				{
					bool flag3 = value[i * 2 + 1];
					if (flag3)
					{
						byte[] array3 = array;
						int num3 = i;
						array3[num3] += 1;
					}
				}
			}
			return array;
		}

		// Token: 0x060008DB RID: 2267 RVA: 0x000327D0 File Offset: 0x000309D0
		internal static byte[] TransByteArrayToAsciiByteArray(byte[] value)
		{
			bool flag = value == null;
			byte[] result;
			if (flag)
			{
				result = new byte[0];
			}
			else
			{
				byte[] array = new byte[value.Length * 2];
				for (int i = 0; i < value.Length / 2; i++)
				{
					SoftBasic.BuildAsciiBytesFrom(BitConverter.ToUInt16(value, i * 2)).CopyTo(array, 4 * i);
				}
				result = array;
			}
			return result;
		}

		// Token: 0x060008DC RID: 2268 RVA: 0x00032830 File Offset: 0x00030A30
		internal static byte[] TransAsciiByteArrayToByteArray(byte[] value)
		{
			byte[] array = new byte[value.Length / 2];
			for (int i = 0; i < array.Length / 2; i++)
			{
				ushort value2 = Convert.ToUInt16(Encoding.ASCII.GetString(value, i * 4, 4), 16);
				BitConverter.GetBytes(value2).CopyTo(array, i * 2);
			}
			return array;
		}

		/// <summary>
		/// 计算Fx协议指令的和校验信息
		/// </summary>
		/// <param name="data">字节数据</param>
		/// <returns>校验之后的数据</returns>
		// Token: 0x060008DD RID: 2269 RVA: 0x00032890 File Offset: 0x00030A90
		internal static byte[] FxCalculateCRC(byte[] data)
		{
			int num = 0;
			for (int i = 1; i < data.Length - 2; i++)
			{
				num += (int)data[i];
			}
			return SoftBasic.BuildAsciiBytesFrom((byte)num);
		}

		/// <summary>
		/// 检查指定的和校验是否是正确的
		/// </summary>
		/// <param name="data">字节数据</param>
		/// <returns>是否成功</returns>
		// Token: 0x060008DE RID: 2270 RVA: 0x000328C8 File Offset: 0x00030AC8
		internal static bool CheckCRC(byte[] data)
		{
			byte[] array = MelsecHelper.FxCalculateCRC(data);
			bool flag = array[0] != data[data.Length - 2];
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = array[1] != data[data.Length - 1];
				result = !flag2;
			}
			return result;
		}
	}
}
